package com.aiju.cash.ui.login;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.aiju.app.DataProviderManager;
import com.aiju.base.activity.BaseActivity;
import com.aiju.cash.R;
import com.aiju.cash.ui.login.manager.LoginCheck;
import com.aiju.cash.ui.login.manager.LoginManager;
import com.aiju.cash.widgit.LineEditText;
import com.aiju.utils.L;
import com.aiju.utils.PermissionUtils;
import com.aiju.utils.PreferencesUtils;
import com.readystatesoftware.systembartint.SystemBarTintManager;

/**
 * @author: 佐为
 * @date: 2016-11-10 14:42
 * @version: V1.0
 * @Description: 登录页面
 */
public class LoginActivity extends BaseActivity {

    private LineEditText phoneEditText, passwordEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        phoneEditText = (LineEditText) findViewById(R.id.et_login_phone);
        passwordEditText = (LineEditText) findViewById(R.id.et_login_password);
        phoneEditText.setLineColor(Color.WHITE);
        passwordEditText.setLineColor(Color.WHITE);

        phoneEditText.setText(PreferencesUtils.getString(this, "name"));
        passwordEditText.setText(PreferencesUtils.getString(this, "password"));
        //登录
        findViewById(R.id.tv_login_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = phoneEditText.getText().toString().trim();
                String password = passwordEditText.getText().toString().trim();
                if (LoginCheck.checkPhone(phone) && LoginCheck.checkPassword(password)) {
                    new LoginManager(LoginActivity.this, mDialogHelper).login(phone, password);
                }
            }
        });

        //跳转忘记密码
        findViewById(R.id.tv_login_rorget).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, CodeActivity.class);
                intent.putExtra("codeType", CodeActivity.FORGET_CODE);
                startActivity(intent);
            }
        });

        //联系我们
        String model = DataProviderManager.getDataProvider().getModel();
        TextView contract = (TextView) findViewById(R.id.tv_phone);
        if ("V1".equals(model))
            contract.setVisibility(View.INVISIBLE);
        contract.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + "0571-89935939"));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });

        if (Build.VERSION.SDK_INT >= 23) {
            PermissionUtils.requestMultiPermissions(LoginActivity.this, new PermissionUtils.PermissionGrant() {
                @Override
                public void onPermissionGranted(int requestCode) {
                    L.d("Permission:" + requestCode);
                }
            });
/*            new PermissionDialog(this).setLayoutParams().setOnSimpleClick(new PermissionDialog.OnSimpleClick() {
                @Override
                public void onCancel() {
                }
                @Override
                public void onSubmit() {

                }
            }).setLayoutParams().show();*/
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        L.d(requestCode);
    }

    @Override
    protected void setStatusBar() {
        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setTintColor(getTitleColor());
        tintManager.setStatusBarAlpha(0);
    }


}
